package com.atguigu.binaryTree;
public class TheSuccessNode {
    public static TreeNode getSuccessTreeNode(TreeNode node){
        if(node==null){
            return node;
        }
        if(node.right!=null){ //有右树
            return getLeftMost(node.right); //获取右树中的最左节点
        }else{ //无右子树
            TreeNode parent=node.parent;
            while(parent!=null && parent.left!=node){ //是树的右孩子
                node=parent;
                parent=node.parent;
            }
            return parent;
        }
    }
    public static TreeNode getLeftMost(TreeNode root){
        if(root==null) return root;
        while(root.left!=null){
            root=root.left;
        }
        return root;
    }
}
